package com.repair.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.entity.ForumPost;
import com.repair.entity.ForumComment;
import com.repair.mapper.ForumPostMapper;
import com.repair.mapper.ForumCommentMapper;
import com.repair.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ForumServiceImpl extends ServiceImpl<ForumPostMapper, ForumPost> implements ForumService {

    @Autowired
    private ForumPostMapper forumPostMapper;

    @Autowired
    private ForumCommentMapper forumCommentMapper;

    @Override
    @Transactional
    public void createPost(ForumPost post) {
        post.setStatus(1); // 设置状态为已发布
        save(post);
    }

    @Override
    public List<ForumPost> getPostList(int page, int size) {
        int offset = (page - 1) * size;
        return forumPostMapper.getPostList(offset, size);
    }

    @Override
    public int getPostCount() {
        return forumPostMapper.getPostCount();
    }

    @Override
    @Transactional
    public void createComment(ForumComment comment) {
        comment.setStatus(1); // 设置状态为已发布
        forumCommentMapper.insert(comment);
    }

    @Override
    public List<ForumComment> getCommentList(Long postId) {
        return forumCommentMapper.getCommentList(postId);
    }

    @Override
    public List<ForumComment> getReplyList(Long parentId) {
        return forumCommentMapper.getReplyList(parentId);
    }

    @Override
    @Transactional
    public void deletePost(Long postId) {
        ForumPost post = new ForumPost();
        post.setId(postId);
        post.setStatus(2); // 设置状态为已删除
        updateById(post);
    }

    @Override
    @Transactional
    public void deleteComment(Long commentId) {
        ForumComment comment = new ForumComment();
        comment.setId(commentId);
        comment.setStatus(2); // 设置状态为已删除
        forumCommentMapper.updateById(comment);
    }
} 